/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.nbpam.components;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.balisunrise.application.MenuFolder;
import org.balisunrise.common.IconManager;
import org.balisunrise.common.MenuAction;
import org.balisunrise.swing.common.Icons;
import org.openide.nodes.*;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author glauco
 */
public class ModuleMenuNode extends AbstractNode{

    private Action action;

    public ModuleMenuNode(MenuAction menuAction) {

        super(Children.LEAF, Lookups.singleton(menuAction));

        setName(menuAction.getName());
        setDisplayName(menuAction.getDescription());
        String iconPath = menuAction.getIcon();
        if(iconPath != null && iconPath.length() > 0)
            setIconBaseWithExtension(iconPath);

        action = new MMNAction(menuAction);
    }

    public ModuleMenuNode(MenuFolder menuFolder){
        super(new ModuleMenuChildren(menuFolder),
                Lookups.singleton(menuFolder));
        setName(menuFolder.getName());
        setDisplayName(menuFolder.getName());
        setIconBaseWithExtension(Icons.getIconPath(IconManager.FOLDER));
    }

    @Override
    public Action getPreferredAction() {
        return action;
    }

    private class MMNAction extends AbstractAction{

        MenuAction menuAction;

        public MMNAction(MenuAction menuAction) {
            this.menuAction = menuAction;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            this.menuAction.getAction().perform(null);
        }

    }
}
